package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.wanmarecruit.entity.Position;
import com.wanmait.wanmarecruit.mapper.PositionMapper;
import com.wanmait.wanmarecruit.service.WorkExperienceTypeService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.entity.WorkExperienceType;
import com.wanmait.wanmarecruit.mapper.WorkExperienceTypeMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class WorkExperienceTypeServiceImpl extends ServiceImpl<WorkExperienceTypeMapper, WorkExperienceType> implements WorkExperienceTypeService {

    @Resource
    private WorkExperienceTypeMapper workExperienceTypeMapper;

    @Resource
    private PositionMapper positionMapper;

    @Override
    public List<WorkExperienceType> getAllWorkExperience() {
        LambdaQueryWrapper<WorkExperienceType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(WorkExperienceType::getId,WorkExperienceType::getWorkExperienceInfo);
        return this.list(queryWrapper);
    }

    @Override
    public List<WorkExperienceType> getAllWorkExperienceAndPositionCount() {
        LambdaQueryWrapper<WorkExperienceType> lambdaQueryWrapper = new LambdaQueryWrapper();
        List<WorkExperienceType> workExperienceTypes = workExperienceTypeMapper.selectList(lambdaQueryWrapper);

        workExperienceTypes.forEach(workExperienceType -> {
            LambdaQueryWrapper<Position> lambdaQueryWrapper1 = new LambdaQueryWrapper();
            lambdaQueryWrapper1.eq(Position::getWorkExperienceTypeId,workExperienceType.getId());
            Integer positionCount = positionMapper.selectCount(lambdaQueryWrapper1).intValue();
            workExperienceType.setPositionCount(positionCount);
        });

        return workExperienceTypes;
    }
}
